#include <cstdlib>
#include <iostream>
#include <cctype>

using namespace std;
const int MAXIMUM_DIGITS = 4;
int size;             // number of digits in the integer
int digit[MAXIMUM_DIGITS]; // the digits stored in reverse order

void introduction();
void input_Large_Int(int a[], int& size_of_A);
void output_Large_Int(int a[], int size_of_A);
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);

int main()
{
	int a[MAXIMUM_DIGITS], b[MAXIMUM_DIGITS], sum[MAXIMUM_DIGITS];
	int size_of_A, size_of_B, size_Sum;
	char ans;
	introduction();
	
	do {
		
		input_Large_Int(a, size_of_A);
		input_Large_Int(b, size_of_B);
		
		add(a, size_of_A, b, size_of_B, sum, size_Sum);
		cout << "The sum of ";
		output_Large_Int(a, size_of_A);
		cout << " and ";
		output_Large_Int(b, size_of_B);
		cout << " is ";
		
		output_Large_Int(sum, size_Sum);
		cout << endl << endl;
		cout << "Would you like to add two more numbers? (y or n): ";
		cin >> ans;
	}
	while (ans == 'y' || ans == 'Y');
	
	system("PAUSE");
	return 0;
}

void introduction()
{
	cout << "This program will allow you to input two large integers and add\n"
	<< "them together. Then you will be allowed to run the program again\n"
	<< "and again depending on how many calculations you would like to make.\n"
	<< endl << endl;
}

void input_Large_Int(int a[], int& size_of_A)
{
	char digit[MAXIMUM_DIGITS];
	char change;
	int i = 0;
	
	cout << "Please enter a positive integer with no more than " << MAXIMUM_DIGITS
	<< " digits: ";
	
	cin.get(change);
	if (change == '\n')
		cin.get(change);
	while (isdigit(change) && i < MAXIMUM_DIGITS)
	{
		digit[i] = change;
		i++;
		cin.get(change);
	}
	
	size_of_A = i;
	int  j = 0;
	while (i > 0)
	{
		i--;
		a[j] = digit[i] - '0';
		j++;
	}
	
	for (int i = j; i < MAXIMUM_DIGITS; i++)
		a[i] = 0;
	
	for (int i = 0; i < MAXIMUM_DIGITS; i++)
		cout<<a[i];
	cout<<endl;
}

void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)
{
	//int i;
	
	cout<<"=================A========== \n";
	for (int i = 0; i < MAXIMUM_DIGITS; i++)
	{cout<<a[i];}
	cout<< endl;
	
	cout<<"=================b========== \n";
	for (int i = 0; i < MAXIMUM_DIGITS; i++)
	{cout<<b[i];}
	cout<< endl;
	
	
	for (int i=0; i < MAXIMUM_DIGITS; i++)
		
	{ 
		sum[i] = (a[i]+b[i]);
		if ((a[i]+b[i])>9)
		{
			a[i+1] = a[i+1] + 1;
			cout<<"a[i]"<<a[i]<<endl;
			cout<<"a[i+1]"<<a[i+1]<<endl;
			sum[i] = sum[i]% 10;
			cout<<"SUM"<<sum[i];
			cout<<endl;
		}
		/*else 
		 {
		 sum[i] = (a[i]+b[i]);
		 //sum[i] = sum[i]% 10;
		 cout<<"SUM else"<<sum[i];
		 cout<<endl;
		 }*/
	}
	/*for(i = 0; i < MAXIMUM_DIGITS - 1; i++);
	 {
	 sum[i] += (a[i] + b[i]) % 10;
	 sum[i + 1] += (a[i] + b[i]) / 10;
	 }
	 i++;
	 sum[i] += (a[i] + b[i]) %10;
	 if ((a[i] + b[i])/10 > 0)
	 {
	 cout << "There was an overflow causing more than 20 integers.\n"
	 << endl;
	 }*/
	cout<<"=================SUM reverse========== \n";
	
	for (int i = 0; i < MAXIMUM_DIGITS; i++)
	{cout<<sum[i];}
	cout<<endl;
	cout<<"=================SUM forward========== \n";
	cout<<"MAXIMUM_DIGITS-1"<<sum[MAXIMUM_DIGITS-1]<<endl;
		
	if (sum[MAXIMUM_DIGITS]== 0)
	{
		for (int i = MAXIMUM_DIGITS-1; i >= 0; i--)
			{	
				cout<<sum[i];
				
			}
			cout<<endl;
	}
	
	else if  (sum[MAXIMUM_DIGITS]== 1)
	{
		for (int i = MAXIMUM_DIGITS; i >= 0; i--)
		{	
			cout<<sum[i];
			
		}
		cout<<endl;
	}
	
	
	else 
	{
		for (int i = MAXIMUM_DIGITS-1; i >= 0; i--)
		{	
			cout<<sum[i];
			
		}
		cout<<endl;
	}
	
	
}
void output_Large_Int(int a[], int size_of_A)
{
	if ( a[size]== 0)
	{
		for (int i = 0; i < size_of_A-1; i++)
			cout << a[size_of_A - i - 1];
	}
	else{
	for (int i = 0; i < size_of_A; i++)
		cout << a[size_of_A - i - 1];
	}
}